var param = {};
var tradeNo = '';
var isGroupBuy = 0;

/**
 * 下单前的检查
 */
function checkForOrder() {
    //埋点——点击“确认支付”
    tar.hitTag('sureBuyClick');

    isGroupBuy = 0;
    if (selectSeats.length < 1) {
        layer.msg('请选择座位');
        return false;
    } else if (selectSeats.length > maxSelect) {
        layer.msg('至多选择20个座位');
        return false;
    } else {
        var post_url = toRoute('api/account/getPersonInfo');
        $.post(post_url, {'token': token}, function (data) {
            if (data.errorCode == '200') {
                if (data.data.isBindingPhone == 0) {
                    yzmShow();
                } else {
                    //下单
                    AjaxPlaceOrder();
                }
            }
        }, 'json');
    }
}

/**
 * 团购下单前的检查
 */
function checkForGroupOrder() {
    //埋点——点击“确认支付”
    tar.hitTag('sureGroupBuyClick');

    isGroupBuy = 1;
    var seatCnt = $('input[name="seatCnt"]').val();
    if (!isIntNumber(seatCnt)) {
        layer.msg('选座数量必须是一个整数');
        return false;
    } else if (seatCnt < 10) {
        layer.msg('至少一次团购10个座位');
        return false;
    } else if (seatCnt > 30) {
        layer.msg('至多一次团购30个座位');
        return false;
    } else {
        var post_url = toRoute('api/account/getPersonInfo');
        $.post(post_url, {'token': token}, function (data) {
            if (data.errorCode == '200') {
                if (data.data.isBindingPhone == 0) {
                    yzmShow();
                } else {
                    //团购下单
                    AjaxGroupPlaceOrder();
                }
            }
        }, 'json');
    }
}

/**********************调用微信JS api 支付*******************/
function jsApiCall()
{
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        param,
        function(res){
            if(res.err_msg == "get_brand_wcpay_request:ok" ){
                // 使用以上方式判断前端返回,微信团队郑重提示：
                //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                //location.href = 'payed.html?tradeNo=' + tradeNo;
                var loading_index = layer.load(1, {
                    shade: [0.1, '#fff'] //0.1透明度的白色背景
                });
                AjaxGetOrderItem(tradeNo, loading_index);
            } else if(res.err_msg == "get_brand_wcpay_request:cancel" ){
                // 取消订单
                AjaxCancelOrder();
            }
        }
    );
}

function callpay()
{
    if (typeof WeixinJSBridge == "undefined"){
        if( document.addEventListener ){
            document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
        }else if (document.attachEvent){
            document.attachEvent('WeixinJSBridgeReady', jsApiCall);
            document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
        }
    }else{
        jsApiCall();
    }
}
/**********************调用微信JS api 支付*******************/

/**
 * ajax请求下单
 * @constructor
 */
function AjaxPlaceOrder() {
    if (selectSeats.length < 1) {
        layer.msg('请选择座位');
        return false;
    } else if (selectSeats.length > maxSelect) {
        layer.msg('至多选择20个座位');
        return false;
    } else {
        var post_url = toRoute('api/order/placeDb');
        var seatIds = selectSeats.join(',');
        $.post(post_url, {'token': token, 'areaId': area_id, 'seatIds': seatIds}, function (data) {
            if (data.errorCode == '200') {
                tradeNo = data.data.tradeNo;
                param = data.data.prePayInfo;
                callpay();
            } else {
                if (data.errorCode == '10007') {
                    layer.msg(data.errorMsg);
                }
            }
        }, 'json');
    }
}

/**
 * ajax请求团购下单
 * @constructor
 */
function AjaxGroupPlaceOrder() {
    var seatCnt = $('input[name="seatCnt"]').val();
    if (!isIntNumber(seatCnt)) {
        layer.msg('选座数量必须是一个整数');
        return false;
    } else if (seatCnt < 10) {
        layer.msg('至少一次团购10个座位');
        return false;
    } else if (seatCnt > 30) {
        layer.msg('至多一次团购30个座位');
        return false;
    } else {
        var post_url = toRoute('api/order/groupPlaceDb');
        $.post(post_url, {'token': token, 'seatCnt': seatCnt}, function (data) {
            if (data.errorCode == '200') {
                tradeNo = data.data.tradeNo;
                param = data.data.prePayInfo;
                callpay();
            } else {
                if (data.errorCode == '10007') {
                    layer.msg(data.errorMsg);
                }
            }
        }, 'json');
    }
}

/**
 * ajax取消订单
 * @constructor
 */
function AjaxCancelOrder() {
    var post_url = toRoute('api/order/cancelDb');
    $.post(post_url, {'token': token, 'tradeNo': tradeNo}, function (data) {
        if (data.errorCode == '200') {
            tradeNo = '';
            param = {};
            AjaxGetSeatInfo(page)
        }
    }, 'json');
}
